<?php

namespace app\admin\model;
use think\Model;
use think\Db;
class RoleModel extends Model
{
    public function __construct()
    {
        parent::__construct();
    }
    //获取权限用户列表
    public function getRoleList()
    {
        $db = Db::table('auth_group');
        return $db->select();
    }
    //获取权限的功能模块
    public function rolePermission()
    {
        $res = Db::table('auth_rule')->where('status', 1)->select();
        $rows = $this->showTree($res);
        return $rows;
    }

    //个人用户拥有的相应权限
    public function getRolePermission()
    {
        $id = input('id');
        return Db::table('auth_group')->where('id', $id)->find();
    }

    //无限极分类
    public function showTree($arr, $html = "&nbsp;&nbsp;&nbsp;&nbsp;", $pid = 0, $level = 0)
    {
        $newArr = array();
        foreach ($arr as $key => $v) {
            if ($v['pid'] == $pid) {
                $v['level'] = $level + 1;
                $v['html'] = str_repeat($html, $level);
                $v['data'] = $this->showTree($arr, $html, $v['id'], $v['level']);
                $newArr[] = $v;
            }
        }
        return $newArr;
    }

    //修改权限
    public function roleUpdate($ids)
    {
        $id = input('id');
        return Db::table('auth_group')
            ->where('id', $id)
            ->update(['rules' => $ids]);
    }

}